package com.card.vault.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.card.vault.domain.another.Card;

@Repository
public class CardDAOImpl implements CardDAO {
	
	@Autowired
    private SessionFactory sessionFactory;
	
	
	public void saveCard(Card card) {
		sessionFactory.getCurrentSession().saveOrUpdate(card);
	}

	@SuppressWarnings("unchecked")
	public List<Card> cardList(int user_id) {
		List<Card> listCard = sessionFactory.getCurrentSession()
    			.createQuery("from Card where USER_ID = " + user_id).list();
        return listCard;
	}

	public void removeCard(int card_id) {
		Card card = (Card) sessionFactory.getCurrentSession().load(Card.class, card_id);
    	if (null != card) {
            sessionFactory.getCurrentSession().delete(card);
        }
	}

	public Card retriveCard(int card_id) {
		Query q = sessionFactory.getCurrentSession().createQuery("from Card where id = :id");
        q.setInteger("id", card_id);
        return (Card) q.uniqueResult();
	}

}
